layui.use(['layer', 'jquery'], function () {
    var layer = layui.layer;
    var $ = layui.$;
    //绑定登录按钮
    $('#loginBtn').on('click', function () {
        if ($("#LAY-user-login-username").val() == '' || $("#LAY-user-login-password").val() == '') {
            layer.msg("请输入用户名或密码");
            return false;
        }
        var params = $("#loginForm").serialize();
        $.ajax({
            url: 'http://localhost:8080/user/login',
            type: 'post',
            data: params,
            success: function (vo) {
                if (vo.code == 401) {
                    layer.msg(vo.message);
                } else if (vo.code == 200) {
                    // 判断此账号的身份是不是为教师或管理员
                    if(vo.data.role==2 || vo.data.role==3){
                        /**
                         * 登录成功则添加当前用户的一条日志记录
                         */
                        let formData = {
                            userId: vo.data.userId,
                            userName: vo.data.userName,
                            realName: vo.data.realName,
                            content: '登录了阿里迪里管理系统'
                        }
                        $.ajax({
                            url: 'http://localhost:8080/log/add',
                            type: 'post',
                            data: formData,
                            success: function (vo) {

                            }
                        });

                        sessionStorage.setItem('admin', JSON.stringify(vo.data));
                        location.href = '../manage/index.html';
                    }else{
                        layer.msg("账号未授权");
                    }
                    
                }
            }
        });
    });
});